use apikit::rpc::{DutyClient, ServerAddressManager};
use bytes::Bytes;
use carpet::base::{
    dto::{FileContent, FileItem},
    param::Multipart,
};
use reqwest::Client;
use template_api::api::demo::file::*;
use tokio::{fs::File, io::AsyncReadExt};

pub struct ServiceManage;

impl ServerAddressManager for ServiceManage {
    fn get_server_addr(&self, _name: &str) -> &str {
        "http://localhost:8030"
    }
}

#[tokio::test]
async fn t() {
    let path = "D:/a.txt";
    let mut file = File::open(path).await.unwrap();
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer).await.unwrap();
    let bytes: Bytes = Bytes::from(buffer);
    println!("len = {}", bytes.len());

    let client = HandleClient::new(Client::new(), ServiceManage);
    let result = client
        ._handle(Multipart(RequestData {
            title: String::from("title"),
            age: Some(1),
            description: vec!["a".into(), "b".into()],
            image: FileItem {
                content: FileContent { bytes },
                file_name: Some(String::from("aa.a")),
                content_type: Some(String::from(
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                )),
            },
        }))
        .await
        .unwrap();
    println!("len = {}", result.bytes().await.unwrap().len());
}
